package com.link2room.jetfire.facade.impl;

import com.link2room.common.constant.BaseConstant;
import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.entity.BaseEntity;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.mns.MNSUtil;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.JacksonUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.jetfire.entity.ReservationAccount;
import com.link2room.jetfire.entity.ReservationBase;
import com.link2room.jetfire.entity.ReservationNmsSta;
import com.link2room.jetfire.constant.ReservationConstant;
import com.link2room.jetfire.dto.param.base.ReservationParamDto;
import com.link2room.jetfire.dto.result.base.ReservationBaseDto;
import com.link2room.jetfire.facade.IReservationTaskFacadeService;
import com.link2room.optimus.constant.BaseRateplanConstant;
import com.link2room.optimus.constant.OptimusMnsConstant;
import com.link2room.soundwave.constant.SoundwaveMnsConstant;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

;

/**
 * Created by sss on 2016/9/20.
 */
@Component("reservationTaskFacadeService")
public class ReservationTaskFacadeServiceImpl implements IReservationTaskFacadeService {

    @Resource
    private MNSUtil mnsUtil;

    @Resource
    private ICommonService commonService;

    /**
     * 取消订单
     * @param baseL2rParamDto
     * @throws Exception
     */
    @Override
    public void cancelReservationBase(BaseL2rParamDto baseL2rParamDto)throws Exception{
        L2RCondition l2RCondition=new L2RCondition();
        l2RCondition.setAndConditions(new ConditionDetail[]{
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ, baseL2rParamDto.getLhotelGroupCode()),
                new ConditionDetail(ReservationBase.ReservationBase_.resSta.toString(), CriteriaUtil.Operator.EQ,ReservationConstant.RESSTA.R),
                new ConditionDetail(ReservationBase.ReservationBase_.resType.toString(), CriteriaUtil.Operator.EQ, BaseRateplanConstant.RESTYPE.PRE),
                new ConditionDetail(ReservationBase.ReservationBase_.confirmSta.toString(), CriteriaUtil.Operator.EQ,ReservationConstant.CONFIRMSTA.NOTCFMD),
                new ConditionDetail(ReservationBase.ReservationBase_.cancelDate.toString(), CriteriaUtil.Operator.LT,new Date())
        });
        QueryResult<ReservationBase> reservationBaseResult=commonService.findAllByCondition(ReservationBase.class,l2RCondition);
        List<ReservationBase> reservationBaseList = reservationBaseResult.getResultlist();
        if(reservationBaseList!=null&&reservationBaseList.size()>0) {
            for (ReservationBase reservationBase : reservationBaseList) {
                ReservationAccount queryRA=new ReservationAccount();
                queryRA.setLhotelGroupCode(reservationBase.getLhotelGroupCode());
                queryRA.setLhotelCode(reservationBase.getLhotelCode());
                queryRA.setL2RNo(reservationBase.getL2RNo());
                ReservationAccount reservationAccount=commonService.findOneEQ(ReservationAccount.class,queryRA);
                ReservationParamDto reservationInsideParamDto =new ReservationParamDto();
                reservationInsideParamDto.setOperType(ReservationConstant.OPERTYPE.CANCEL);
                reservationInsideParamDto.setReservationBase(ClassConverUtil.copyPropertiesToDto(reservationBase,ReservationBaseDto.class, false));
                if(ReservationConstant.L2RPAYSTA.NONE.equals(reservationAccount.getL2rPaysta())){
                    reservationBase.setConfirmSta(ReservationConstant.CONFIRMSTA.CANCELING);
                    commonService.merge(reservationBase);
                    ReservationNmsSta reservationNmsSta2=new ReservationNmsSta();
                    reservationNmsSta2.setLhotelGroupCode(reservationBase.getLhotelGroupCode());
                    reservationNmsSta2.setLhotelCode(reservationBase.getLhotelCode());
                    reservationNmsSta2.setL2RNo(reservationBase.getL2RNo());
                    reservationNmsSta2.setItem(BaseConstant.ServiceCategoryConstant.OPTIMUS);
                    reservationNmsSta2.setOperType(ReservationConstant.OPERTYPE.CANCEL);
                    commonService.save(reservationNmsSta2);

                    mnsUtil.putMessageToQueue(OptimusMnsConstant.QUEUE_ORDER_OPTIMUS,JacksonUtil.beanToJson(reservationInsideParamDto));
                }
                if(StringUtil.isNotBlank(reservationBase.getCrsNo())){
                    ReservationNmsSta reservationNmsSta2=new ReservationNmsSta();
                    reservationNmsSta2.setLhotelGroupCode(reservationBase.getLhotelGroupCode());
                    reservationNmsSta2.setLhotelCode(reservationBase.getLhotelCode());
                    reservationNmsSta2.setL2RNo(reservationBase.getL2RNo());
                    reservationNmsSta2.setItem(BaseConstant.ServiceCategoryConstant.SOUNDWAVE);
                    reservationNmsSta2.setOperType(ReservationConstant.OPERTYPE.CANCEL);
                    commonService.save(reservationNmsSta2);
                    mnsUtil.putMessageToQueue(SoundwaveMnsConstant.QUEUE_ORDER_SOUNDWAVE,JacksonUtil.beanToJson(reservationInsideParamDto));
                }
                //mnsUtil.putMessageToQueue(MnsConstant.QUEUE_ORDER_SOUNDWAVE,JacksonUtil.beanToJson(reservationInsideParamDto));
            }
        }
    }




}
